package com.cgtong.android.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.cgtong.android.R;

public class AvatarAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private Context con;
    private TypedArray avatars;

    public AvatarAdapter(Context context) {
        this.con = context;
        mInflater = LayoutInflater.from(context);
        avatars = con.getResources().obtainTypedArray(R.array.avatars);
    }

    @Override
    public int getCount() {
        return avatars.length();
    }

    @Override
    public Object getItem(int position) {
        return avatars.getResourceId(position, 0);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            convertView = mInflater.inflate(R.layout.adapter_my_avatar, null);
            holder = new ViewHolder();
            holder.iv_avatar = (ImageView) convertView.findViewById(R.id.iv_avatar);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.iv_avatar.setImageResource(avatars.getResourceId(position, 0));
        return convertView;
    }


    private static class ViewHolder {
        ImageView iv_avatar;
    }

}
